package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;


    @Override
    public void submit(Orders orders) {
//        A. 获得当前用户id, 查询当前用户的购物车数据
        Long userId = orders.getUserId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findByUid(userId);
//        B. 根据当前登录用户id, 查询用户数据
        User user = userMapper.findById(userId);

//        C. 根据地址ID, 查询地址数据
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());

//        D. 组装订单明细数据, 批量保存订单明细
        long orderId = UUIDUtils.getUUIDInOrderId().longValue();
        //声明变量记录总金额
        BigDecimal totalAmount = new BigDecimal("0");

        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //按下alt+enter
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDInOrderId().longValue()); //订单项目的主键
            orderDetail.setName(shoppingCart.getName());  //购物项的菜品的名字
            orderDetail.setOrderId(orderId); //订单项所属的订单的id
            orderDetail.setDishId(shoppingCart.getDishId()); //购物项的菜品的id
            orderDetail.setSetmealId(shoppingCart.getSetmealId()); //购物项的套餐的id
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor()); //口味
            orderDetail.setNumber(shoppingCart.getNumber());  //数量
            orderDetail.setAmount(shoppingCart.getAmount()); //单价
            orderDetail.setImage(shoppingCart.getImage()); //
            //计算每一个购物项的总价
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber() + ""));
            totalAmount = totalAmount.add(amount);
            orderDetailList.add(orderDetail);
        }
        //        E. 组装订单数据, 批量保存订单数据
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId)); //订单号
        orders.setStatus(1); //状态
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(totalAmount);
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());

        //插入订单
        orderMapper.save(orders);
        //批量插入订单项
        orderDetailMapper.saveBatch(orderDetailList);


//        F. 删除当前用户的购物车列表数据
        shoppingCartMapper.removeByUid(userId);
    }

    @Override
    public Page<OrdersDto> getUserPage(Integer page, Integer pageSize, Long userId) {
        PageHelper.startPage(page,pageSize);
        List<Orders> ordersList = orderMapper.getNewPageByUid(userId);
        PageInfo<Orders> pageInfo = new PageInfo<>(ordersList);

        List<OrdersDto> ordersDtoList = ordersList.stream().map(orders -> {

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            ordersDto.setOrderDetails(orderDetailMapper.findByOid(orders.getId()));
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> ordersPage = new Page<>(ordersDtoList,page,pageSize,pageInfo.getTotal(),pageInfo.getPages());
        return ordersPage;
    }

    @Override
    public Page<Orders> getOrdersPage(Integer page, Integer pageSize,String number, String beginTime, String endTime) {

        PageHelper.startPage(page,pageSize);
        List<Orders> ordersList = orderMapper.findByNumberAnd(number,beginTime,endTime);
        PageInfo<Orders> pageInfo = new PageInfo<>(ordersList);
        Page<Orders> pageResult = new Page<>(ordersList,page,pageSize,pageInfo.getTotal());
        return pageResult;
    }
}
